﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using YUIAspNet;

namespace YUIAspNetDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (!IsPostBack)
                this.IsCPLoaded = false;

            if (IsCPLoaded)
            {
                this.LoadCP();
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (!IsCPLoaded)
                this.btn.Text = "Load Color Picker";
            else
                this.btn.Text = "Unload Color Picker";


            base.OnPreRender(e);
        }

        protected void LoadCP()
        {
            ColorPicker picker = new ColorPicker();
            picker.ID = "picker";
            picker.CssClass = "colorPickerAjax";
            this.pnlUpdate.ContentTemplateContainer.Controls.Add(picker);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.btn.Click += new EventHandler(btn_Click);

           
        }

        public bool IsCPLoaded
        {
            get { return Convert.ToBoolean(Session["loaded"]); }
            set { Session["loaded"] = value; }
        }

        void btn_Click(object sender, EventArgs e)
        {
            if (!IsCPLoaded)
            {
                this.LoadCP();
            }
            else
            {
                // do nothing, it won't be in the response unless I load it.
            }

            IsCPLoaded = !IsCPLoaded;
        }
    }
}
